﻿//-----------------------------------------------------------------------
// <copyright file="Helpers.cs" company="AFS">
// Copyright (c) AFS. All rights reserved.
//
//    This file is part of Chatbox.
//
//    Chatbox is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    any later version.
//
//    Chatbox is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//    GNU Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public License
//    along with Chatbox. If not, see http://www.gnu.org/copyleft/lesser.html
// </copyright>
//----------------------------------------------------------------------- 

namespace ChatBox.Utility
{
    using System;
    using System.Xml.Linq;

    /// <summary>
    /// Generic and general-purpose exrtension and helper methods 
    /// </summary>
    public static class Helpers
    {
        /// <summary>
        /// convert a string to int 
        /// </summary>
        /// <param name="value">the string</param>
        /// <returns>the interger value fronm the string</returns>
        public static int ToInteger(this string value)
        {
            int result;
            bool success = int.TryParse(value, out result);

            if (success)
            {
                return result;
            }

            return 0;
        }

        /// <summary>
        /// Convert an XAttribute to boolean
        /// </summary>
        /// <param name="attribute">the attribute</param>
        /// <returns>the boolean value, or false if the attribute was not present</returns>
        public static bool ToBoolean(this XAttribute attribute)
        {
            if (attribute == null)
            {
                return false;
            }

            return attribute.Value.ToBoolean();
        }

        /// <summary>
        /// convert a string to bool 
        /// </summary>
        /// <param name="value">the string</param>
        /// <returns>the interger value fronm the string</returns>
        public static bool ToBoolean(this string value)
        {
            if (value.Equals("0", StringComparison.OrdinalIgnoreCase))
            {
                return false;
            }
            
            if (value.Equals("1", StringComparison.OrdinalIgnoreCase))
            {
                return true;
            }
            bool result;
            bool success = bool.TryParse(value, out result);

            if (success)
            {
                return result;
            }

            return false;
        }

        /// <summary>
        ///  convert a string to a datetime
        /// </summary>
        /// <param name="value">the value to convert</param>
        /// <returns>the dateime</returns>
        public static DateTime ToDateTime(this string value)
        {
            DateTime result;
            bool success = DateTime.TryParse(value, out result);

            if (success)
            {
                return result;
            }

            return DateTime.MinValue;
        }

        /// <summary>
        /// convert a string to a decimal
        /// </summary>
        /// <param name="value">the value to convert</param>
        /// <returns>the decimal</returns>
        public static decimal ToDecimal(this string value)
        {
            decimal result;
            bool success = decimal.TryParse(value, out result);

            if (success)
            {
                return result;
            }

            return 0;
        }

        /// <summary>
        /// Dispose of an object if it is disposable
        /// </summary>
        /// <param name="value">the object to test and dispose</param>
        public static void TestDispose(this object value)
        {
            if (value != null)
            {
                IDisposable disposable = value as IDisposable;
                if (disposable != null)
                {
                    disposable.Dispose();
                }
            }
        }
    }
}
